package org.jeecg.modules.yc.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.yc.web.entity.PdDevice;
import org.jeecg.modules.yc.web.entity.YcDeviceReplaceRecords;
import org.jeecg.modules.yc.web.entity.vo.ReplaceDeviceModelVo;
import org.jeecg.modules.yc.web.service.IPdDeviceService;
import org.jeecg.modules.yc.web.service.IYcDeviceReplaceRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * @Description: 业务处理
 * @Author: jeecg-boot
 * @Date:   2025-02-21
 * @Version: V1.0
 */
@Api(tags="业务处理")
@RestController
@RequestMapping("/yc/web/ycBusiness")
@Slf4j
public class BusinessController {
    @Autowired
    private IPdDeviceService ycPdDeviceService;
    @Autowired
    private IYcDeviceReplaceRecordsService ycDeviceReplaceRecordsService;

    /**
     *  编辑
     *
     * @param replaceDeviceModelVo
     * @return
     */
    @AutoLog(value = "设备更换记录-编辑")
    @ApiOperation(value="设备更换", notes="设备更换")
    @RequiresPermissions("yc.web:yc_device_replace_records:replaceDevice")
    @RequestMapping(value = "/replaceDevice", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> replaceDevice(@RequestBody ReplaceDeviceModelVo replaceDeviceModelVo) {
        // 获取要替换的设备信息
        PdDevice pdDevice = ycPdDeviceService.getById(replaceDeviceModelVo.getId());
        // 创建设备替换记录对象
        YcDeviceReplaceRecords ycDeviceReplaceRecords = createDeviceReplaceRecords(replaceDeviceModelVo);

        // 处理新设备编号的逻辑
        if (shouldUseOldDeviceNo(replaceDeviceModelVo)) {
            // 使用旧设备编号
            ycDeviceReplaceRecords.setOldDeviceNo(replaceDeviceModelVo.getNo());
            ycDeviceReplaceRecords.setNewDeviceNo(replaceDeviceModelVo.getNo());
            updateDeviceInfo(replaceDeviceModelVo, ycDeviceReplaceRecords);
        } else {
            // 检查同一终端下是否存在相同设备编号
            if (isDuplicateDeviceNo(pdDevice, replaceDeviceModelVo)) {
                return Result.error("同一终端下不允许相同设备编号");
            }
            // 更新设备信息
            updateDeviceInfo(replaceDeviceModelVo, ycDeviceReplaceRecords);
            ycDeviceReplaceRecords.setNewDeviceNo(replaceDeviceModelVo.getNewDeviceNo());
            ycDeviceReplaceRecords.setOldDeviceNo(replaceDeviceModelVo.getNo());
            ycDeviceReplaceRecords.setTerminalId(pdDevice.getTerminalId());
            ycDeviceReplaceRecords.setCableJointId(pdDevice.getCableJointId());
        }

        // 保存设备替换记录
        ycDeviceReplaceRecordsService.save(ycDeviceReplaceRecords);
        return Result.OK("编辑成功!");
    }

    // 创建设备替换记录对象
    private YcDeviceReplaceRecords createDeviceReplaceRecords(ReplaceDeviceModelVo replaceDeviceModelVo) {
        YcDeviceReplaceRecords records = new YcDeviceReplaceRecords();
        records.setReplaceDeviceId(replaceDeviceModelVo.getId());
        records.setOldDeviceName(replaceDeviceModelVo.getName());
        records.setNewDeviceName(StringUtils.isBlank(replaceDeviceModelVo.getNewDeviceName())
                ? replaceDeviceModelVo.getName()
                : replaceDeviceModelVo.getNewDeviceName());
        return records;
    }

    // 判断是否使用旧设备编号
    private boolean shouldUseOldDeviceNo(ReplaceDeviceModelVo replaceDeviceModelVo) {
        return StringUtils.isBlank(replaceDeviceModelVo.getNewDeviceNo()) ||
                replaceDeviceModelVo.getNo().equals(replaceDeviceModelVo.getNewDeviceNo());
    }

    // 更新设备信息
    private void updateDeviceInfo(ReplaceDeviceModelVo replaceDeviceModelVo, YcDeviceReplaceRecords ycDeviceReplaceRecords) {
        PdDevice pdDeviceUpdate = new PdDevice();
        pdDeviceUpdate.setId(ycDeviceReplaceRecords.getReplaceDeviceId());
        pdDeviceUpdate.setName(StringUtils.isBlank(replaceDeviceModelVo.getNewDeviceName()) ? null :replaceDeviceModelVo.getNewDeviceName());
        pdDeviceUpdate.setNo(replaceDeviceModelVo.getNewDeviceNo());
        ycPdDeviceService.updateById(pdDeviceUpdate);
    }

    // 检查同一终端下是否存在相同设备编号
    private boolean isDuplicateDeviceNo(PdDevice pdDevice, ReplaceDeviceModelVo replaceDeviceModelVo) {
        QueryWrapper<PdDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("terminal_id", pdDevice.getTerminalId());
        queryWrapper.eq("no", replaceDeviceModelVo.getNewDeviceNo());
        List<PdDevice> pdDevices = ycPdDeviceService.list(queryWrapper);
        return !CollectionUtils.isEmpty(pdDevices);
    }

}